<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2018/12/27
 * Time: 10:06
 */
namespace app\factory\controller;
use think\Controller;
use think\Db;
class Login extends Controller
{
    //厂家登录页面
    public function index(){
        return $this->fetch('/login');
    }

    //验证密码和账号
    public function doLogin(){
        extract(input());
        $hasUser = Db::name('factory f')
            ->join('province p','f.f_id=p.p_id','left')
            ->where('factory_name',$username)
            ->whereOr('factory_account',$username)
            ->field(['factory_id','factory_name','f_area','factory_account','factory_password','p_username'])
            ->find();
        if(empty($hasUser)){
            return json(['code' => -1, 'url' => '', 'msg' => '厂家不存在',"status"=>100]);
        }


        if(md5($password) != $hasUser['factory_password']){
            return json(['code' => -2, 'url' => '', 'msg' => '密码错误',"status"=>100]);
        }
        session('f_uid',$hasUser['factory_id']);
        session('f_username',$hasUser['factory_name']);
        session('f_account',$hasUser['factory_account']);
        session('f_area',$hasUser['f_area']);
        session('p_username',$hasUser['p_username']);
        return json(['code' => 1, 'url' => url('factory/Index/index'), 'msg' => '登录成功！']);
    }
    //退出操作
    public function loginOut()
    {
        session(null);
        $this->redirect(url('factory/index/index'));
    }

}